/*
 * @Author: szx
 * @Date: 2022-02-16 14:18:13
 * @LastEditTime: 2022-02-16 14:29:31
 * @Description:
 * @FilePath: \leetcode\1-99\27\27_2.js
 */
/**
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
// var removeElement = function (nums, val) {
//     let slow = 0;
//     for (let num of nums) {
//         if (num !== val) nums[slow++] = num;
//     }
//     nums.length = slow;
//     return slow;
// };

var removeElement = function (nums, val) {
    let left = 0,
        right = nums.length - 1;
    while (left <= right) {
        if (nums[left] === val) {
            nums[left] = nums[right];
            right--;
        } else {
            left++;
        }
    }
    return left;
};
console.log(removeElement([3, 2, 2, 3], 3));
